/**
 * @(#)BaseController.java, 2017-12-03.
 * <p>
 * Copyright 2017 Youdao, Inc. All rights reserved.
 * YOUDAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.irs.irsforum.web.common;


import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.irs.irsforum.entity.Student;
import com.irs.irsforum.entity.User;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;

/**
 * BaseController
 * <p>
 * @author yihuili
 *
 * @since 2020-01-18
 */
public abstract class BaseController {

    protected static final String USER_TOKEN_ATTRIBUTE = "USER_TOKEN_ATTR";

    protected static final String OBJECT_USER_ATTRIBUTE = "OBJECT_USER";


    private static final Logger logger =
            LoggerFactory.getLogger(BaseController.class);


    @ModelAttribute(OBJECT_USER_ATTRIBUTE)
    protected Long getObjectUserId(HttpServletRequest request) {
        User user = getUserId();
        if (user == null || !user.isAuthenticated()) {
            return null;
        }
        return user.getId();
    }

    @ModelAttribute(USER_TOKEN_ATTRIBUTE)
    protected User getUserId() {
        return (User) SecurityContextHolder.getContext()
                .getAuthentication();
    }

}
